package ui.panel.setup;

import cerebrum.common.ConfigPropertiesUtil;
import cerebrum.common.EmptyChecker;
import cerebrum.common.OsUtil;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.DirectoryChooser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ui.factory.SingletonFactory;
import ui.panel.setup.subsidiary.ScreenSaverSetUpStage;
import ui.util.Window;

import java.io.File;

public class VideoSetUpStage extends Window {
    private Logger log = LoggerFactory.getLogger(VideoSetUpStage.class);
    private Pane rootPane = new Pane();
    private Pane tips = new Pane();
    private Label downloadTips = new Label();
    private Label downloadAddress = new Label();
    private static VideoSetUpStage instance = null;

    //调用单例工厂
    public static VideoSetUpStage getInstance() {
        if (instance == null) {
            instance = SingletonFactory.getWeakInstace(VideoSetUpStage.class);
        }
        return instance;
    }

    public Pane getSetUpPane() {
        rootPane.setLayoutX(390);
        rootPane.setLayoutY(200);
        rootPane.setPrefHeight(200);
        rootPane.setPrefWidth(730);
        /**
         * 使用一个Pane作为背景，一防止mac壁纸颜色和菜单颜色一致时混淆视觉
         * 二，让pane半透明时其他组件不受影响
         */
        Pane background = new Pane();
        background.setPrefHeight(200);
        background.setPrefWidth(730);
        background.setStyle("-fx-background-color: #fffef9");
        background.setOpacity(0.7);

        ImageView backgroundImage = new ImageView(new Image("/fxml/css/images/setep/video_setup.png"));
        backgroundImage.setFitWidth(900);
        backgroundImage.setFitHeight(300);
        backgroundImage.setLayoutY(-50);
        backgroundImage.setLayoutX(-80);
        rootPane.getChildren().addAll(background,backgroundImage);

        if (!OsUtil.checkIsInstallVlc()) {
            noInstallVSLTips();
        } else {
            userSetup();
        }

        return rootPane;
    }

    private void userSetup() {
        Pane setUp = new Pane();
        setUp.setId("fileSetUp");
        setUp.setPrefWidth(600);
        setUp.setPrefHeight(150);
        setUp.setLayoutX(30);

        downloadAddress.setText("》 》》 》》 》 》 视频壁纸点此下载 《《 《 《");
        downloadAddress.setLayoutY(10);
        downloadAddress.setLayoutX(-15);
        downloadAddress.setTextFill(Paint.valueOf("#54a5ff"));
        downloadAddress.setFont(new Font("宋体", 14));
        downloadAddress.setOnMouseExited(event -> {
            downloadAddress.setTextFill(Paint.valueOf("54a5ff"));
            downloadAddress.setUnderline(false);
        });
        downloadAddress.setOnMouseEntered(event -> {
            downloadAddress.setTextFill(Paint.valueOf("#009ad6"));
            downloadAddress.setUnderline(true);
        });

        ImageView textbox = new ImageView(new Image("/fxml/css/images/setep/video_setup_textbox.png"));
        textbox.setFitHeight(75);
        textbox.setFitWidth(420);
        textbox.setLayoutY(60);
        textbox.setLayoutX(60);

        TextField field = new TextField(); // 创建一个单行输入框
        field.setLayoutX(85);
        field.setLayoutY(85);
        field.setPrefSize(355, 35); // 设置单行输入框的推荐宽高
        field.setEditable(true); // 设置单行输入框能否编辑
        field.setPromptText("请选择视频壁纸所保存目录"); // 设置单行输入框的提示语
        field.setAlignment(Pos.CENTER_LEFT); // 设置单行输入框的对齐方式
        field.setPrefColumnCount(11); // 设置单行输入框的推荐列数

        field.setOnMouseClicked(event -> {
            //调用系统文件选择组件
            DirectoryChooser directoryChooser = new DirectoryChooser();
            File file = directoryChooser.showDialog(this);
            if (EmptyChecker.notEmpty(file)) {
                String path = file.getAbsolutePath();
                field.setText(path);
                //设置光标位置
                field.positionCaret(path.length());
            }

        });

        Button button = new Button("开 始 体 验");
        button.setLayoutX(505);
        button.setLayoutY(77);
        button.setPrefHeight(47);
        button.setPrefWidth(110);
        button.setTextFill(Paint.valueOf("#2376b7"));
        button.setOnMouseClicked(event -> {
            String videoPath = field.getText();
            if (EmptyChecker.isEmpty(videoPath)){
                field.setPromptText("请选择视频壁纸所在目录!");
            }else {
                try {
                    File file = new File(videoPath);
                    File[] files = file.listFiles();
                    if (EmptyChecker.isEmpty(files)) {
                        field.setText("");
                        field.setPromptText("别想忽悠我，该目录下空空如也。。。");
                    }else {
                        //将配置持久化
                        ConfigPropertiesUtil.set("wallpaper_type","video");
                        ConfigPropertiesUtil.set("video_path",videoPath);
                        log.info("设置壁纸为：视频壁纸，文件路径：{}",videoPath);
                        //将此页面抬走
                        removeNode(rootPane,"fileSetUp");
                        //加入新的页面
                        rootPane.getChildren().add(ScreenSaverSetUpStage.getInstance().getPane());
                    }
                }catch (Exception e) {
                    field.setText("");
                    field.setPromptText("请选择视频壁纸所在目录!");
                    e.printStackTrace();
                }

            }
        });

        setUp.getChildren().addAll(textbox, field, button, downloadAddress);
        rootPane.getChildren().add(setUp);
    }

    /**
     * vsl播放器检测
     */
    private void noInstallVSLTips() {
        tips.setId("tips");
        tips.setPrefWidth(600);
        tips.setPrefHeight(150);
        tips.setLayoutX(30);
//        tips.setStyle("-fx-background-color: #e5344d");
        ImageView backgroundImage = new ImageView(new Image("/fxml/css/images/setep/video_setup_tips.png"));
        backgroundImage.setFitHeight(80);
        backgroundImage.setFitWidth(485);
        backgroundImage.setLayoutY(55);
        backgroundImage.setLayoutX(40);

        Button button = new Button("  重 新 检 测  ");
        button.setLayoutX(490);
        button.setLayoutY(75);
        button.setStyle("-fx-shape: 'M2368.893896 1024l-1650.774288-1.02724L0 0h2022.714098l346.179798 1024z'; -fx-background-color: #54a5ff");
        button.setPrefHeight(47);
        button.setPrefWidth(140);
        button.setTextFill(Paint.valueOf("WHITE"));

        downloadTips.setText("请先安装VLC media player播放器,点击下载。。。");
        downloadTips.setLayoutY(98);
        downloadTips.setLayoutX(180);
        downloadTips.setTextFill(Paint.valueOf("WHITE"));
        downloadTips.setOnMouseExited(event -> {
            downloadTips.setTextFill(Paint.valueOf("WHITE"));
            downloadTips.setUnderline(false);
        });
        downloadTips.setOnMouseEntered(event -> {
            downloadTips.setTextFill(Paint.valueOf("#2c2c2c"));
            downloadTips.setUnderline(true);
        });

        tips.getChildren().addAll(backgroundImage, downloadTips, button);
        rootPane.getChildren().add(tips);

        button.setOnMouseExited(event -> {
            button.setTextFill(Paint.valueOf("WHITE"));
            button.setUnderline(false);
        });

        button.setOnMouseEntered(event -> {
            button.setTextFill(Paint.valueOf("#2c2c2c"));
            button.setUnderline(true);
        });
        //点击再次检测如果已经安装了vlc播放器就把提示pane给清除掉
        button.setOnMouseClicked(event -> {
            if (OsUtil.checkIsInstallVlc()) {
                tips.getChildren().clear();
                rootPane.getChildren().remove(tips);
                userSetup();
            }
        });
    }

    //点击按钮打开网站
    public void openSourceURL(Application application) {
        //Install vlc video player Download Address
        downloadTips.setOnMouseClicked(event ->
                application.getHostServices().showDocument("https://www.videolan.org/")
        );

        //Video Wallpaper Download Address
        downloadAddress.setOnMouseClicked(event ->
                application.getHostServices().showDocument("http://bbs.huoying666.com/forum-53-1.html")
        );
    }
}
